/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jsalon.web.controller;
import jamjun.infra.annotation.Param;
import jamjun.infra.exception.ErrorContainer;
import jamjun.infra.exception.ErrorModel;
import jamjun.infra.exception.JJException;
import jamjun.infra.exception.JJValidateExeption;
import jamjun.infra.model.ActionType;
import jamjun.infra.model.Paging;
import jamjun.infra.model.UserSession;
import jamjun.infra.web.CHelper;
import static jamjun.infra.web.CHelper.MappingRequestToObj;
import jamjun.infra.web.Content;
import jamjun.infra.web.Controller;
import jamjun.infra.web.GSession;
import jamjun.infra.web.Json;
import jamjun.infra.web.JsonResult;
import jamjun.infra.web.Result;
import jamjun.infra.web.View;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jsalon.enumtype.Gender;
import jsalon.model.Customer;
import jsalon.model.CustomerFilter;
import jsalon.repo.CustomerRepo;
import jsalon.repo.SysNumberRepo;
import jsalon.repo.UserSessionRepo;
import jsalon.web.vm.CustomerVM;
/**
 *
 * @author Java
 */
@WebServlet(name = "customer", urlPatterns = {"/customer"})
public class CustomerController extends Controller{
    public static Result index(@Param(name = "menuID") String menuID) 
    {
        CustomerVM viewModel = new CustomerVM();
        jsalon.model.Customer customer = new jsalon.model.Customer();
        try {
            //<editor-fold desc="mapping Model.">
            customer = MappingRequestToObj(customer,"data",GSession.getRequest());
            //customerList = CHelper.MappingRequestToObjList(customer,"data", GSession.getRequest());
            //customerList = MappingRequestToObjList
            viewModel.customer = customer;
            viewModel.customer.code="xxxx";
            viewModel.menuID = menuID;
            //</editor-fold>
            //uuid
            
            return new View("/View/Customer/index.jsp",viewModel);
        } catch (Exception ex) {
            Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            return new Content(ex.getMessage());
        }        
    }
    public static Result list()
    {
        CustomerVM viewModel = new CustomerVM();
        List<jsalon.model.Customer> customerList;
        jsalon.model.CustomerFilter modelFilter = new CustomerFilter();
        try {
            //<editor-fold desc="mapping Model.">
            modelFilter = MappingRequestToObj(modelFilter,"data",GSession.getRequest());
            int pageNo = Integer.parseInt(GSession.getRequest().getParameter("pageNo"));
            int PageSize = Integer.parseInt(GSession.getRequest().getParameter("PageSize"));
            
            //customerList = CHelper.MappingRequestToObjList(customer,"data", GSession.getRequest());
            //customerList = MappingRequestToObjList
            //</editor-fold>
            //repo get List
            jamjun.infra.model.Paging paging = new Paging(pageNo, PageSize);
            jsalon.repo.CustomerRepo customerRepo = new CustomerRepo();
            modelFilter.domainID = "D201411-01";
            jsalon.model.Customer customer = new Customer();
            customerList = customerRepo.getList(paging,modelFilter);
            viewModel.paging = paging;
            viewModel.customerList = customerList;
            if(viewModel.customerList == null){
                viewModel.customerList = new ArrayList<>();
            }
            return new View("/View/Customer/list.jsp",viewModel);
        } catch (Exception ex) {
            Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            return new Content(ex.getMessage());
        }        
        
    }
    public static Result detail(
            @Param(name = "actionType") String actionType,
            @Param(name = "customerCode") String customerCode)
    {
        CustomerVM viewModel = new CustomerVM();
        List<jsalon.model.Customer> customerList;
        jsalon.model.CustomerFilter modelFilter = new CustomerFilter();
        try {
            //<editor-fold desc="mapping Model.">
            modelFilter = MappingRequestToObj(modelFilter,"data",GSession.getRequest());
            viewModel.actionType = ActionType.valueOf(GSession.getRequest().getParameter("actionType"));
            //String customerCode = GSession.getRequest().getParameter("customerCode");
            //String actionType = GSession.getRequest().getParameter("actionType");
            //</editor-fold>
            //repo get List
            viewModel.customer = new Customer();
            if(viewModel.actionType != ActionType.insert){
                jamjun.infra.model.Paging paging = new Paging(0,1);
                jsalon.repo.CustomerRepo customerRepo = new CustomerRepo();
                modelFilter.domainID = GSession.getUserSession().domainID;
                modelFilter.code = customerCode;
                customerList = customerRepo.getList(paging,modelFilter);
                viewModel.paging = paging;
                if(customerList != null && customerList.size() > 0){
                    viewModel.customer = customerList.get(0);
                }
            }else{
                UUID uuid = UUID.randomUUID();
                //get customer code
                jsalon.repo.SysNumberRepo sysNumberRepo = new jsalon.repo.SysNumberRepo();
                String code = sysNumberRepo.getCode(GSession.getUserSession().domainID,jsalon.enumtype.Doctype.customer);
                viewModel.customer.code = code;
                viewModel.customer.uuid = uuid.toString();
                viewModel.customer.gender = Gender.man;
                viewModel.customer.registerDate = new Date();
            }
            return new View("/View/Customer/detail.jsp",viewModel);
        } catch (Exception ex) {
            Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            return new Content(ex.getMessage());
        }        
        
    }
    
    public static Result save(
            @Param(name="data") Customer data
            ,@Param(name="actionType")ActionType actionType)
    {
        JsonResult jsonResult = new JsonResult();
        try{
            //<editor-fold desc="mapping Model.">
            //ActionType actionTypeEnum = ActionType.valueOf(GSession.getRequest().getParameter("actionType"));
            //customer = MappingRequestToObj(customer,"data",GSession.getRequest());
            //</editor-fold>
            jsalon.repo.CustomerRepo customerRepo = new CustomerRepo();
            //get user session
            data.domainID = GSession.getUserSession().domainID;
            data.createBy = GSession.getUserSession().userID;
            data.updateBy = data.createBy;
            if(actionType == actionType.insert){
                data = customerRepo.insert(data);
            }else{
                data = customerRepo.update(data);
            }
            jsonResult.success = true;
            return new Json(jsonResult);
        }catch(JJValidateExeption vex){
            jsonResult.success = false;
            jsonResult.errorContainer = vex.errorContainer;
            return new Json(jsonResult);
        }catch (JJException jjex){
            jsonResult.success = false;
            jsonResult.errorContainer = jjex.errorContainer;
            return new Json(jsonResult);
        }catch (Exception ex) {
            Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            jsonResult = new jamjun.infra.exception.ExHelper().ConvertToJsonResult(ex);
            return new Json(jsonResult);
        }    
    }
    
    public static Result delete(@Param(name="code") String code)
    {
        JsonResult jsonResult = new JsonResult();
        try{
            //<editor-fold desc="mapping Model.">
            String domainID = GSession.getUserSession().domainID;
            String userID = GSession.getUserSession().userID;
            //String code = GSession.getRequest().getParameter("code");
            ActionType actionTypeEnum = ActionType.valueOf(GSession.getRequest().getParameter("actionType"));
            //</editor-fold>
            jsalon.repo.CustomerRepo customerRepo = new CustomerRepo();
            boolean success = false;
            if(actionTypeEnum == actionTypeEnum.delete){
                success = customerRepo.delete(domainID,code,userID);
            }
            jsonResult.success = success;
            return new Json(jsonResult);
        }catch(JJValidateExeption vex){
            jsonResult.success = false;
            jsonResult.errorContainer = vex.errorContainer;
            return new Json(jsonResult);
        }catch (JJException jjex){
            jsonResult.success = false;
            jsonResult.errorContainer = jjex.errorContainer;
            return new Json(jsonResult);
        }catch (Exception ex) {
            Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            jsonResult = new jamjun.infra.exception.ExHelper().ConvertToJsonResult(ex);
            return new Json(jsonResult);
        }    
    }
    
    public static Result content()
    {   
        return new Content("Hello Java");
    }
}
